package com.feng.cloud.system.controller;

import com.feng.cloud.common.annotation.ControllerEndpoint;
import com.feng.cloud.common.exception.system.ParameterException;
import com.feng.cloud.common.exception.system.SystemStatus;
import com.feng.cloud.common.request.QueryRequest;
import com.feng.cloud.common.response.RestResponse;
import com.feng.cloud.common.response.TableResultResponse;
import com.feng.cloud.system.entity.GeneratorConfig;
import com.feng.cloud.system.service.GeneratorConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GeneratorConfigController
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/23 15:08
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("generatorConfig")
@RequiredArgsConstructor
public class GeneratorConfigController {

    private final GeneratorConfigService generatorConfigService;

    /**
     * 新增配置
     * @param generatorConfig
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('gen:config:add')")
    public RestResponse<GeneratorConfig> add(@Valid GeneratorConfig generatorConfig) {
        generatorConfigService.save(generatorConfig);
        return new RestResponse<GeneratorConfig>().data(generatorConfig);
    }

    /**
     * 获取服务配置列表
     * @param tableSchema tableSchema
     * @return List<GeneratorConfig>
     */
    @GetMapping("service/{tableSchema}")
    @PreAuthorize("hasAuthority('gen:generate')")
    public RestResponse<List<GeneratorConfig>> selectServiceList(@NotBlank(message = "{required}")@PathVariable String tableSchema) {
        return new RestResponse<List<GeneratorConfig>>().data(generatorConfigService.selectServiceList(tableSchema));
    }

    /**
     * 校验配置是否存在
     * @param serviceName serviceName
     * @return RestResponse<Void>
     */
    @GetMapping("check/{serviceName}")
    public RestResponse<Void> checkGenConfig(@NotBlank(message = "{required}")
                                             @PathVariable String serviceName) {
        GeneratorConfig generatorConfig = generatorConfigService.selectGenConfigByServiceName(serviceName);
        if (generatorConfig == null) {
            return new RestResponse(SystemStatus.GENERATOR_CONFIG_NULL);
        }
        return new RestResponse<>();
    }


    /**
     * 分页查询配置
     * @param params params
     * @return TableResultResponse<GeneratorConfig>
     */
    @GetMapping("page")
    @PreAuthorize("hasAuthority('gen:config:view')")
    public TableResultResponse<GeneratorConfig> selectPage(@RequestParam Map<String, Object> params) {
        QueryRequest request = new QueryRequest(params);
        return generatorConfigService.selectPage(request);
    }

    /**
     * 修改配置
     * @param generatorConfig generatorConfig
     */
    @PutMapping
    @PreAuthorize("hasAuthority('gen:config:update')")
    @ControllerEndpoint(operation = "修改生成代码配置", exceptionMessage = "修改GeneratorConfig失败")
    public void updateGeneratorConfig(@Valid GeneratorConfig generatorConfig) {
        Long id = generatorConfig.getId();
        if (id == null) {
            throw new ParameterException(SystemStatus.ID_EMPTY);
        }
        generatorConfigService.updateGeneratorConfig(generatorConfig);
    }
}
